package com.example.excelutil.service.impl;

import com.example.excelutil.service.ExcelService;
import com.example.excelutil.util.ExcelUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Excel服务实现类
 */
@Service
@Slf4j
public class ExcelServiceImpl implements ExcelService {

    @Override
    public List<String> extractColumnByIndex(MultipartFile file, int columnIndex) {
        return extractColumnByIndex(file, columnIndex, 0);
    }

    @Override
    public List<String> extractColumnByIndex(MultipartFile file, int columnIndex, int sheetIndex) {
        try {
            // 确保输入流支持mark/reset操作
            return ExcelUtil.extractColumn(file.getInputStream(), columnIndex, sheetIndex);
        } catch (IOException e) {
            log.error("提取Excel列数据失败: {}", e.getMessage(), e);
            return new ArrayList<>();
        }
    }

    @Override
    public List<String> extractColumnByName(MultipartFile file, String columnName) {
        return extractColumnByName(file, columnName, 0);
    }

    @Override
    public List<String> extractColumnByName(MultipartFile file, String columnName, int sheetIndex) {
        try {
            // 确保输入流支持mark/reset操作
            return ExcelUtil.extractColumnByName(file.getInputStream(), columnName, sheetIndex);
        } catch (IOException e) {
            log.error("提取Excel列数据失败: {}", e.getMessage(), e);
            return new ArrayList<>();
        }
    }
}